package com.yndsoft.jc.ui.categorypage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ProductType;
import com.yndsoft.jc.ui.categorypage.ProductListActivity;
import com.yndsoft.jc.ui.categorypage.entity.BaseListItem;
import com.yndsoft.jc.utils.ConstantValues;
import com.yndsoft.jc.widget.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/12/4.
 */

public class CategoryAdapter extends BaseAdapter {

    private Context mContext;
    private List<ProductType> data = new ArrayList<>();

    public CategoryAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<ProductType> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        View viewItem2 = null;
//        int itemType = this.getItemViewType(position);
//
//        if (itemType == ViewHolder1.ITEM_VIEW_TYPE) {
//            //第一种item
//            ViewHolder1 viewHolder1 = null;
//            if (convertView == null) {
//                viewHolder1 = new ViewHolder1();
//                viewItem1 = LayoutInflater.from(mContext).inflate(R.layout.item_categorylist1, null);
//                viewHolder1.tv_address_name = viewItem1.findViewById(R.id.tv_address_name);
//                viewItem1.setTag(viewHolder1);
//                convertView = viewItem1;
//            } else {
//                viewHolder1 = (ViewHolder1) convertView.getTag();
//            }
//
//            viewHolder1.tv_address_name.setText(data.get(position).getItem().getName());
//        } else if (itemType == ViewHolder2.ITEM_VIEW_TYPE) {
        //第二种item
        ViewHolder2 viewHolder2 = null;
        if (convertView == null) {
            viewHolder2 = new ViewHolder2();
            viewItem2 = LayoutInflater.from(mContext).inflate(R.layout.item_categorylist2, null);
            viewHolder2.tv_address_name = viewItem2.findViewById(R.id.tv_address_name);
            viewHolder2.category_list = viewItem2.findViewById(R.id.category_list);
            viewItem2.setTag(viewHolder2);
            convertView = viewItem2;
        } else {
            viewHolder2 = (ViewHolder2) convertView.getTag();
        }
        viewHolder2.tv_address_name.setText(data.get(position).getName());
        final ViewHolder2 finalViewHolder = viewHolder2;
        if (data.get(position).getDets() != null || data.get(position).getDets().size() != 0) {
            viewHolder2.category_list.setAdapter(new GridAdapter(data.get(position).getDets(), mContext));
        }
        viewHolder2.category_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext, ProductListActivity.class);
                intent.putExtra(ConstantValues.PRODUCT_ID, data.get(position).getDets().get(i).getId());
                mContext.startActivity(intent);
            }
        });
//        }

        return convertView;
    }

    public final class ViewHolder1 {
        public static final int ITEM_VIEW_TYPE = 0;
        public TextView tv_address_name;
    }

    public final class ViewHolder2 {
        public static final int ITEM_VIEW_TYPE = 1;
        public MyGridView category_list;
        public TextView tv_address_name;
    }

}
